package com.example.beryl.produce;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.example.beryl.R;
import com.example.beryl.proSystem.finishWork;
import com.example.beryl.proSystem.productionReport;
import com.example.beryl.proSystem.cutingUpdate;
import com.example.beryl.proSystem.cuttingReceive;
import com.example.beryl.proSystem.cuttingStart;
import com.example.beryl.proSystem.startWork;
import java.util.ArrayList;
import java.util.HashMap;
/**
 * 登录后各个账号进入的首页
 */
public class produceIndex extends AppCompatActivity {
    private GridView gridView;
    private TextView textView;
    private Toolbar toolbar;
    private ConstraintLayout clMain;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.produce_index);
        //textView = (TextView) findViewById(R.id.text_title);
        Intent intent = getIntent();
        String nn=intent.getStringExtra("info1");

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        clMain = (ConstraintLayout) findViewById(R.id.cl_main);

        setSupportActionBar(toolbar);
        toolbar.setTitle(nn);

        toolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                AlertDialog.Builder builder = new AlertDialog.Builder(produceIndex.this);
                builder.setTitle("信息");
                builder.setMessage("是否返回主页");
                builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                      finish();
                    }
                });
                builder.setNegativeButton("否",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
            }
        });

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getGroupId()){
                    case 0:
                    case 1:
                    case 2:
                    case 3:{
                        Toast.makeText(produceIndex.this, "点了一下"+item.getTitle(), Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
                return false;
            }
        });

        gridView = (GridView) findViewById(R.id.gridView1);
        //ArrayList<HashMap<String,Object>> lstImageItem = new ArrayList<HashMap<String,Object>>();
        SimpleAdapter saImageItems = new SimpleAdapter(this,
                               getPage(),R.layout.activity_podcut,
                               new String[]{"ItemImage","ItemText"},
                             new int[] {R.id.ItemImage,R.id.ItemText});
                gridView.setAdapter(saImageItems);
                //点击跳转
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int index = i + 1;
                if (index == 1)
                {
                    Intent intent = new Intent();
                    intent.setClass(produceIndex.this, proStart.class);
                    startActivity(intent);
                }
                else if (index == 2)
                {

                } else if (index == 3)
                {
                    Intent intent = new Intent();
                    intent.setClass(produceIndex.this, startWork.class);
                    startActivity(intent);

                }else if (index == 4)
                {
                    //实现跳转
                    Intent intent = new Intent();
                    intent.setClass(produceIndex.this, cuttingStart.class);
                    startActivity(intent);
                } else if (index == 5) {

                    Intent intent = new Intent();
                    intent.setClass(produceIndex.this, productionReport.class);
                    startActivity(intent);

                } else if (index == 8) {
                    //实现跳转
                    Intent intent = new Intent();
                    intent.setClass(produceIndex.this, cuttingReceive.class);
                    startActivity(intent);

                } else if (index == 9) {
                    Intent intent = new Intent();
                    intent.setClass(produceIndex.this, cutingUpdate.class);
                    startActivity(intent);
                }
                else if (index == 10) {
                    Intent intent = new Intent();
                    intent.setClass(produceIndex.this, finishWork.class);
                    startActivity(intent);
                }
            }
        });
    }
    //得到相应的页面

//状态栏菜单显示
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_menu, menu);
        //MenuItem menuItem = menu.findItem(R.id.toolbar_user);
            //menuItem.setVisible(false);
        setVisible(true);
        return true;
    }

    public  ArrayList<HashMap<String,Object>> getPage()
    {
        ArrayList<HashMap<String,Object>> lstImageItem = new ArrayList<HashMap<String,Object>>();
        //由MainActivity传过来的值，转去那个页面
        Intent intent = getIntent();
        String nn=intent.getStringExtra("info1");
        switch (nn)
        {
            case "生产系统首页":
                for (int i = 0; i<12; i++){
                    HashMap<String,Object> map = new HashMap<String,Object>();
                    if (i == 0)
                    {
                        map.put("ItemImage",R.drawable.tuil);
                        map.put("ItemText","退料申请");
                    }
                    else if (i == 1)
                    {
                        map.put("ItemImage",R.drawable.bujianchuli);
                        map.put("ItemText","补料申请");
                    }
                    else if (i == 2)
                    {
                        map.put("ItemImage",R.drawable.zhiliang);
                        map.put("ItemText","开工记录");
                    }
                    else if (i == 3)
                    {
                        map.put("ItemImage",R.drawable.cangchu);
                        map.put("ItemText","裁切开工记录");
                    }
                    else if (i == 4)
                    {
                        map.put("ItemImage",R.drawable.chaibao);
                        map.put("ItemText","生产报工");
                    }
                    else if (i == 5)
                    {
                        map.put("ItemImage",R.drawable.buliaochuku);
                        map.put("ItemText","批次信息记录");
                    }
                    else if (i == 6)
                    {
                        map.put("ItemImage",R.drawable.ruku);
                        map.put("ItemText","裁切条码打印");
                    }
                    else if (i == 7)
                    {
                        map.put("ItemImage",R.drawable.buliaochuku);
                        map.put("ItemText","裁切接收");
                    }
                    else if (i == 8)
                    {
                        map.put("ItemImage",R.drawable.shebei);
                        map.put("ItemText","裁切报工");
                    }
                    else if (i == 9)
                    {
                        map.put("ItemImage",R.drawable.tuiliaoruk);
                        map.put("ItemText","完工报工");
                    }
                    else if (i == 10)
                    {
                        map.put("ItemImage",R.drawable.yuanliaoruk);
                        map.put("ItemText","设备运行记录");
                    }
                    else if (i == 11)
                    {
                        map.put("ItemImage",R.drawable.caiqiejieshou);
                        map.put("ItemText","异常安灯");
                    }
                    lstImageItem.add(map);
                }
                break;
            case "仓储系统首页":
                for (int i = 0; i<12; i++){
                    HashMap<String,Object> map = new HashMap<String,Object>();
                    if (i == 0)
                    {
                        map.put("ItemImage",R.drawable.tuil);
                        map.put("ItemText","/");
                    }
                    else if (i == 1)
                    {
                        map.put("ItemImage",R.drawable.bujianchuli);
                        map.put("ItemText","补料申请");
                    }
                    else if (i == 2)
                    {
                        map.put("ItemImage",R.drawable.zhiliang);
                        map.put("ItemText","开工记录");
                    }
                    else if (i == 3)
                    {
                        map.put("ItemImage",R.drawable.cangchu);
                        map.put("ItemText","裁切开工记录");
                    }
                    else if (i == 4)
                    {
                        map.put("ItemImage",R.drawable.chaibao);
                        map.put("ItemText","生产报工");
                    }
                    else if (i == 5)
                    {
                        map.put("ItemImage",R.drawable.buliaochuku);
                        map.put("ItemText","批次信息记录");
                    }
                    else if (i == 6)
                    {
                        map.put("ItemImage",R.drawable.ruku);
                        map.put("ItemText","裁切条码打印");
                    }
                    else if (i == 7)
                    {
                        map.put("ItemImage",R.drawable.buliaochuku);
                        map.put("ItemText","物料防错");
                    }
                    else if (i == 8)
                    {
                        map.put("ItemImage",R.drawable.shebei);
                        map.put("ItemText","裁切报工");
                    }
                    else if (i == 9)
                    {
                        map.put("ItemImage",R.drawable.tuiliaoruk);
                        map.put("ItemText","完工报工");
                    }
                    else if (i == 10)
                    {
                        map.put("ItemImage",R.drawable.yuanliaoruk);
                        map.put("ItemText","设备运行记录");
                    }
                    else if (i == 11)
                    {
                        map.put("ItemImage",R.drawable.caiqiejieshou);
                        map.put("ItemText","异常安灯");
                    }
                    lstImageItem.add(map);
                }
                break;
        }
        return lstImageItem;
    }

    //检查用户在这个页面返回时候，提示用户是否要返回登录界面
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        DialogInterface.OnClickListener diaListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int buttonId) {
                switch (buttonId) {
                    case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序
                        finish();
                        break;
                    case AlertDialog.BUTTON_NEGATIVE:// "确认"按钮退出程序
                        //什么都不做
                        break;
                    default:
                        break;
                }
            }
        };
        //检查用户按的是哪个按键
        if (keyCode==KeyEvent.KEYCODE_BACK) {
            //back key Constant Value: 4 (0x00000004)
            //创建退出对话框
            AlertDialog.Builder isExit=new AlertDialog.Builder(this);
            //设置对话框标题
            isExit.setTitle("消息提醒");
            //设置对话框消息
            isExit.setMessage("确定要返回登录页面？");
            // 添加选择按钮并注册监听
            isExit.setPositiveButton("确定",diaListener);
            isExit.setNegativeButton("取消",diaListener);
            //对话框显示
            isExit.show();
        }
        return false;
    }
}